<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;

/**
 * [游戏入口类]
 */
class GameController extends Controller
{    
    /**
     * [请求二十一点游戏大厅]
     * @return {[page] [二十一点游戏大厅]
     */
    public function getEsyhall(Request $req)
    {
	if (!$req->session()->get('user_id') || !$req->session()->get('user_nick')) {
            return redirect('user/login');         
        }  
	
	$res = DB::table('user_games')->where('uid',$req->session()->get('user_id'))->first();
	if ($res->user_esywin + $res->user_esylose == 0) {
            $res = 0;
        } else {
            $res = $res->user_esywin / ($res->user_esywin + $res->user_esylose);
	    $res = round($res, 2) * 100;
        }
        $data = [
            'user_id'    =>  $req->session()->get('user_id'),
            'user_name'  =>  $req->session()->get('user_name'),
            'user_nick'  =>  $req->session()->get('user_nick'),
            'game_name'  =>  '21点',
            'game_id'    =>  '21-',
            'point'      =>  $res, //要查出自己的胜率 !!!
        ];
        return view('game.esyhall',$data);
    }

    /**
     * [请求二十一点房间]
     * @return {[page] [二十一点游戏房间]
     */
    public function getEsyroom(Request $req)
    {
        if (!$req->session()->get('user_id') || !$req->session()->get('user_nick')) {
            return redirect('user/login');
        }
        $data = [
              'user_id'    =>  $req->session()->get('user_id'),
              'user_name'  =>  $req->session()->get('user_name'),
              'user_nick'  =>  $req->session()->get('user_nick'),
          ];
        return view('game.esyroom',$data);
    }

    /**
     * [请求斗地主游戏大厅]
     * @return {[page] [斗地主游戏大厅]
     */
    public function getDdzhall(Request $req)
    {
        if (!$req->session()->get('user_id') || !$req->session()->get('user_nick')) {
            return redirect('user/login');
        }  
        $res = DB::table('user_games')->where('uid',$req->session()->get('user_id'))->first();
        if ($res->user_ddzwin + $res->user_ddzlose == 0) {
            $res = 0;
        } else {
            $res = $res->user_ddzwin / ($res->user_ddzwin + $res->user_ddzlose);
            $res = round($res, 2) * 100;
        }

        $data = [
              'user_id'    =>  $req->session()->get('user_id'),
              'user_name'  =>  $req->session()->get('user_name'),
              'user_nick'  =>  $req->session()->get('user_nick'),
              'game_name'  =>  '斗地主',
              'game_id'    =>  'DZ-',
              'point'      =>  $res,
          ];
          return view('game.dizhuhall',$data);
    }

    /**
     * [请求斗地主游戏房间]
     * @return {[page] [斗地主游戏房间]
     */
    public function getDdzroom(Request $req)
    {
        if (!$req->session()->get('user_id') || !$req->session()->get('user_nick')) {
            return redirect('user/login');
        }  
        $data = [
              'user_id'    =>  $req->session()->get('user_id'),
              'user_name'  =>  $req->session()->get('user_name'),
              'user_nick'  =>  $req->session()->get('user_nick'),
          ];
        return view('game.dizhuroom',$data);

    }

    /**
     * [请求象棋大厅]
     * @return {[page] [象棋大厅]
     */
    public function getXqhall()
    {
      return '象棋游戏大厅';
    }

    /**
     * [请求象棋房间]
     * @return {[page [象棋游戏房间]
     */
    public function getXqroom()
    {
      return '象棋游戏房间';
    }

    /**
     * [请求五子棋游戏大厅]
     * @return {[page] [五子棋游戏大厅]
     */
    public function getWzqhall()
    {
      return '五子棋游戏大厅';
    }

    /**
     * [请求五子棋游戏房间]
     * @return {[page] [五子棋游戏房间]
     */
    public function getWzqroom()
    {
      return '五子棋游戏房间';
    }

    /**
     * [请求踩白块入口]
     * @return {[page] [踩白块入口]
     */
    public function getCbkcenter(Request $req)
    {
        $data = [
          'user_id'    =>  $req->session()->get('user_id'),
          'user_name'  =>  $req->session()->get('user_name'),
          'user_nick'  =>  $req->session()->get('user_nick'),
          'game_name'  =>  '别踩百块',
          'game_id'    =>  'cbk-',
          'point'      =>  '1000', //要查出自己的胜率 !!!
          'url'        =>  '',
        ];
        return view('game.esyhall',$data);
    }

    /**
     * [请求打僵尸入口]
     * @return {[page] [打僵尸入口]
     */
    public function getDjscenter(Request $req)
    {
        $data = [
          'user_id'    =>  $req->session()->get('user_id'),
          'user_nick'  =>  $req->session()->get('user_nick'),
          'end_url'    =>  '/grade/postZombies',
        ];
        return view('game.zombies',$data);
    }
}
